-- SlashThemes.lua — QoL slash commands for Colors tab & themes
local ADDON, DFFT = ...
if not DFFT or not DFFT.DB then return end
local DB = DFFT.DB

-- ------------- helpers -------------
local function Print(msg)
  DEFAULT_CHAT_FRAME:AddMessage("|cff88ff88SCT|r "..tostring(msg))
end
local function ListKeys(tbl)
  local keys = {}
  for k in pairs(tbl or {}) do table.insert(keys, k) end
  table.sort(keys)
  return keys
end
local function FindInsensitive(tbl, name)
  if not tbl or not name then return end
  local lname = name:lower()
  for k in pairs(tbl) do
    if k:lower() == lname then return k end
  end
end
local function RandomKey(tbl)
  local ks = ListKeys(tbl or {})
  if #ks == 0 then return nil end
  return ks[math.random(#ks)]
end

-- ------------- open Colors tab -------------
local function OpenColorsTab()
  if not DFFT.toggleOptions then
    Print("选项界面不可用。")
    return
  end
  DFFT.toggleOptions()
  -- Essaye de basculer vers l’onglet 2 (Colors) si l’UI de tes options l’expose
  -- Beaucoup d’implémentations n’exposent pas les boutons ; on se contente d’ouvrir.
end

-- ------------- THEMES : damage -------------
local function ApplyDamageThemeByName(name)
  if DFFT.DamageThemes then
    local exact = FindInsensitive(DFFT.DamageThemes, name)
    if exact then
      if DFFT.ApplyDamageTheme then DFFT.ApplyDamageTheme(exact) end
      return true
    end
  end
  -- Fallback si Options.lua fournit une table locale via DB (cas improbable ici)
  if DFFT.ApplyDamageTheme and type(name)=="string" then
    DFFT.ApplyDamageTheme(name)  -- laisser gérer l’erreur éventuelle
    return true
  end
  return false
end

local function ListDamageThemes()
  local ks = ListKeys(DFFT.DamageThemes or {})
  if #ks == 0 then
    Print("未找到伤害主题。")
    return
  end
  Print("伤害主题: |cffffffaa"..table.concat(ks, "|r, |cffffffaa").."|r")
end

-- ------------- THEMES : healer -------------
local function ApplyHealThemeByName(name)
  if DFFT.HealThemes then
    local exact = FindInsensitive(DFFT.HealThemes, name)
    if exact then
      if DFFT.ApplyHealTheme then DFFT.ApplyHealTheme(exact) end
      return true
    end
  end
  if DFFT.ApplyHealTheme and type(name)=="string" then
    DFFT.ApplyHealTheme(name)
    return true
  end
  return false
end

local function ListHealThemes()
  local ks = ListKeys(DFFT.HealThemes or {})
  if #ks == 0 then
    Print("未找到治疗主题。")
    return
  end
  Print("治疗主题: |cffffffaa"..table.concat(ks, "|r, |cffffffaa").."|r")
end

-- ------------- Slash router -------------
SLASH_SCT1, SLASH_SCT2 = "/sct", "/sleekcombattext"
SlashCmdList.SCT = function(msg)
  msg = (msg or ""):gsub("^%s+", ""):gsub("%s+$","")
  if msg == "" or msg == "help" then
    Print("命令列表:")
    Print("/sct colors — 打开颜色选项卡")
    Print("/sct theme <名称> — 应用伤害主题")
    Print("/sct themerandom — 应用随机伤害主题")
    Print("/sct listthemes — 列出伤害主题")
    Print("/sct healtheme <名称> — 选择治疗主题")
    Print("/sct listhealthemes — 列出治疗主题")
    return
  end

  local cmd, rest = msg:match("^(%S+)%s*(.*)$")
  cmd = (cmd or ""):lower()

  if cmd == "colors" then
    OpenColorsTab()
    return
  elseif cmd == "theme" then
    if rest == "" then
      Print("用法: /sct theme <名称>  (尝试 /sct listthemes)")
      return
    end
    if ApplyDamageThemeByName(rest) then
      Print("伤害主题已应用: "..rest)
    else
      Print("未知的伤害主题: "..rest.."  (尝试 /sct listthemes)")
    end
    return
  elseif cmd == "themerandom" then
    local rnd = RandomKey(DFFT.DamageThemes or {})
    if rnd then
      if DFFT.ApplyDamageTheme then DFFT.ApplyDamageTheme(rnd) end
      Print("随机伤害主题已应用: "..rnd)
    else
      Print("没有可用的伤害主题。")
    end
    return
  elseif cmd == "listthemes" then
    ListDamageThemes()
    return
  elseif cmd == "healtheme" then
    if rest == "" then
      Print("用法: /sct healtheme <名称>  (尝试 /sct listhealthemes)")
      return
    end
    if ApplyHealThemeByName(rest) then
      Print("治疗主题已选择: "..rest)
    else
      Print("未知的治疗主题: "..rest.."  (尝试 /sct listhealthemes)")
    end
    return
  elseif cmd == "listhealthemes" then
    ListHealThemes()
    return
  end

  Print("未知命令。输入 |cffffffaa/sct help|r 查看帮助。")
end
